home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1991 / 09 / one_hash.lst < prev    next >
File List  |  1991-08-21  |  12KB  |  308 lines

  1. _ONE-WAY HASH FUNCTIONS_
  2. by Bruce Schneier
  3.  
  4. [LISTIN╟ ONE]
  5.  
  6.  
  7. /***********************************************************************
  8.  ** md5.c -- the source code for MD5 routines                         **
  9.  ** RSA Data Security, Inc. MD5 Message-Digest Algorithm              **
  10.  ** Created: 2/17/90 RLR                                              **
  11.  ** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version                   **
  12.  ** Revised (for MD5): RLR 4/27/91                                    **
  13.  ***********************************************************************
  14.  ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved.  **
  15.  ** License to copy and use this software is granted provided that    **
  16.  ** it is identified as the "RSA Data Security, Inc. MD5 Message-     **
  17.  ** Digest Algorithm" in all material mentioning or referencing this  **
  18.  ** software or this function.                                        **
  19.  ** License is also granted to make and use derivative works          **
  20.  ** provided that such works are identified as "derived from the RSA  **
  21.  ** Data Security, Inc. MD5 Message-Digest Algorithm" in all          **
  22.  ** material mentioning or referencing the derived work.              **
  23.  ** RSA Data Security, Inc. makes no representations concerning       **
  24.  ** either the merchantability of this software or the suitability    **
  25.  ** of this software for any particular purpose.  It is provided "as  **
  26.  ** is" without express or implied warranty of any kind.              **
  27.  ** These notices must be retained in any copies of any part of this  **
  28.  ** documentation and/or software.                                    **
  29.  **********************************************************************/
  30.  
  31. #include "md5.h"
  32.  
  33. /* forward declaration */
  34. static void Transform ();
  35.  
  36. static unsigned char PADDING[64] = {
  37.   0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  45. };
  46.  
  47. /* F, G, H and I are basic MD5 functions */
  48. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  49. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  50. #define H(x, y, z) ((x) ^ (y) ^ (z))
  51. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  52.  
  53. /* ROTATE_LEFT rotates x left n bits */
  54. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  55.  
  56. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
  57. /* Rotation is separate from addition to prevent recomputation */
  58. #define FF(a, b, c, d, x, s, ac) \
  59.   {(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  60.    (a) = ROTATE_LEFT ((a), (s)); \
  61.    (a) += (b); \
  62.   }
  63. #define GG(a, b, c, d, x, s, ac) \
  64.   {(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  65.    (a) = ROTATE_LEFT ((a), (s)); \
  66.    (a) += (b); \
  67.   }
  68. #define HH(a, b, c, d, x, s, ac) \
  69.   {(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  70.    (a) = ROTATE_LEFT ((a), (s)); \
  71.    (a) += (b); \
  72.   }
  73. #define II(a, b, c, d, x, s, ac) \
  74.   {(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  75.    (a) = ROTATE_LEFT ((a), (s)); \
  76.    (a) += (b); \
  77.   }
  78.  
  79. /* The routine MD5Init initializes the message-digest context
  80.    mdContext. All fields are set to zero«  */
  81. void MD5Init (mdContext)
  82. MD5_CTX *mdContext;
  83. {
  84.   mdContext->i[0] = mdContext->i[1] = (UINT4)0;
  85.  
  86.   /* Load magic initialization constants.
  87.    */
  88.   mdContext->buf[0] = (UINT4)0x67452301;
  89.   mdContext->buf[1] = (UINT4)0xefcdab89;
  90.   mdContext->buf[2] = (UINT4)0x98badcfe;
  91.   mdContext->buf[3] = (UINT4)0x10325476;
  92. }
  93.  
  94. /* The routine MD5Update updates the message-digest context to
  95.    account for the presence of each of the characters inBuf[0..inLen-1]
  96.    in the message whose digest is being computed«  */
  97. void MD5Update (mdContext, inBuf, inLen)
  98. MD5_CTX *mdContext;
  99. unsigned char *inBuf;
  100. unsigned int inLen;
  101. {
  102.   UINT4 in[16];
  103.   int mdi;
  104.   unsigned int i, ii;
  105.  
  106.   /* compute number of bytes mod 64 */
  107.   mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
  108.  
  109.   /* update number of bits */
  110.   if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0])
  111.     mdContext->i[1]++;
  112.   mdContext->i[0] += ((UINT4)inLen << 3);
  113.   mdContext->i[1] += ((UINT4)inLen >> 29);
  114.  
  115.   while (inLen--) {
  116.     /* add new character to buffer, increment mdi */
  117.     mdContext->in[mdi++] = *inBuf++;
  118.  
  119.     /* transform if necessary */
  120.     if (mdi == 0x40) {
  121.       for (i = 0, ii = 0; i < 16; i++, ii += 4)
  122.         in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
  123.                 (((UINT4)mdContext->in[ii+2]) << 16) |
  124.                 (((UINT4)mdContext->in[ii+1]) << 8) |
  125.                 ((UINT4)mdContext->in[ii]);
  126.       Transform (mdContext->buf, in);
  127.       mdi = 0;
  128.     }
  129.   }
  130. }
  131.  
  132. /* The routine MD5Final terminates the message-digest computation and
  133.    ends with the desired message digest in mdContext->digest[0...15].  */
  134. void MD5Final (mdContext)
  135. MD5_CTX *mdContext;
  136. {
  137.   UINT4 in[16];
  138.   int mdi;
  139.   unsigned int i, ii;
  140.   unsigned int padLen;
  141.  
  142.   /* save number of bits */
  143.   in[14] = mdContext->i[0];
  144.   in[15] = mdContext->i[1];
  145.  
  146.   /* compute number of bytes mod 64 */
  147.   mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
  148.  
  149.   /* pad out to 56 mod 64 */
  150.   padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
  151.   MD5Update (mdContext, PADDING, padLen);
  152.  
  153.   /* append length in bits and transform */
  154.   for (i = 0, ii = 0; i < 14; i++, ii += 4)
  155.     in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
  156.             (((UINT4)mdContext->in[ii+2]) << 16) |
  157.             (((UINT4)mdContext->in[ii+1]) << 8) |
  158.             ((UINT4)mdContext->in[ii]);
  159.   Transform (mdContext->buf, in);
  160.  
  161.   /* store buffer in digest */
  162.   for (i = 0, ii = 0; i < 4; i++, ii += 4) {
  163.     mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
  164.     mdContext->digest[ii+1] =
  165.       (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
  166.     mdContext->digest[ii+2] =
  167.       (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
  168.     mdContext->digest[ii+3] =
  169.       (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
  170.   }
  171. }
  172.  
  173. /* Basic MD5 step. Transforms buf based on in«  */
  174. static void Transform (buf, in)
  175. UINT4 *buf;
  176. UINT4 *in;
  177. {
  178.   UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
  179.  
  180.   /* Round 1 */
  181. #define S11 7
  182. #define S12 12
  183. #define S13 17
  184. #define S14 22
  185.   FF ( a, b, c, d, in[ 0], S11, 0xd76aa478); /* 1 */
  186.   FF ( d, a, b, c, in[ 1], S12, 0xe8c7b756); /* 2 */
  187.   FF ( c, d, a, b, in[ 2], S13, 0x242070db); /* 3 */
  188.   FF ( b, c, d, a, in[ 3], S14, 0xc1bdceee); /* 4 */
  189.   FF ( a, b, c, d, in[ 4], S11, 0xf57c0faf); /* 5 */
  190.   FF ( d, a, b, c, in[ 5], S12, 0x4787c62a); /* 6 */
  191.   FF ( c, d, a, b, in[ 6], S13, 0xa8304613); /* 7 */
  192.   FF ( b, c, d, a, in[ 7], S14, 0xfd469501); /* 8 */
  193.   FF ( a, b, c, d, in[ 8], S11, 0x698098d8); /* 9 */
  194.   FF ( d, a, b, c, in[ 9], S12, 0x8b44f7af); /* 10 */
  195.   FF ( c, d, a, b, in[10], S13, 0xffff5bb1); /* 11 */
  196.   FF ( b, c, d, a, in[11], S14, 0x895cd7be); /* 12 */
  197.   FF ( a, b, c, d, in[12], S11, 0x6b901122); /* 13 */
  198.   FF ( d, a, b, c, in[13], S12, 0xfd987193); /* 14 */
  199.   FF ( c, d, a, b, in[14], S13, 0xa679438e); /* 15 */
  200.   FF ( b, c, d, a, in[15], S14, 0x49b40821); /* 16 */
  201.  
  202.   /* Round 2 */
  203. #define S21 5
  204. #define S22 9
  205. #define S23 14
  206. #define S24 20
  207.   GG ( a, b, c, d, in[ 1], S21, 0xf61e2562); /* 17 */
  208.   GG ( d, a, b, c, in[ 6], S22, 0xc040b340); /* 18 */
  209.   GG ( c, d, a, b, in[11], S23, 0x265e5a51); /* 19 */
  210.   GG ( b, c, d, a, in[ 0], S24, 0xe9b6c7aa); /* 20 */
  211.   GG ( a, b, c, d, in[ 5], S21, 0xd62f105d); /* 21 */
  212.   GG ( d, a, b, c, in[10], S22,  0x2441453); /* 22 */
  213.   GG ( c, d, a, b, in[15], S23, 0xd8a1e681); /* 23 */
  214.   GG ( b, c, d, a, in[ 4], S24, 0xe7d3fbc8); /* 24 */
  215.   GG ( a, b, c, d, in[ 9], S21, 0x21e1cde6); /* 25 */
  216.   GG ( d, a, b, c, in[14], S22, 0xc33707d6); /* 26 */
  217.   GG ( c, d, a, b, in[ 3], S23, 0xf4d50d87); /* 27 */
  218.   GG ( b, c, d, a, in[ 8], S24, 0x455a14ed); /* 28 */
  219.   GG ( a, b, c, d, in[13], S21, 0xa9e3e905); /* 29 */
  220.   GG ( d, a, b, c, in[ 2], S22, 0xfcefa3f8); /* 30 */
  221.   GG ( c, d, a, b, in[ 7], S23, 0x676f02d9); /* 31 */
  222.   GG ( b, c, d, a, in[12], S24, 0x8d2a4c8a); /* 32 */
  223.  
  224.   /* Round 3 */
  225. #define S31 4
  226. #define S32 11
  227. #define S33 16
  228. #define S34 23
  229.   HH ( a, b, c, d, in[ 5], S31, 0xfffa3942); /* 33 */
  230.   HH ( d, a, b, c, in[ 8], S32, 0x8771f681); /* 34 */
  231.   HH ( c, d, a, b, in[11], S33, 0x6d9d6122); /* 35 */
  232.   HH ( b, c, d, a, in[14], S34, 0xfde5380c); /* 36 */
  233.   HH ( a, b, c, d, in[ 1], S31, 0xa4beea44); /* 37 */
  234.   HH ( d, a, b, c, in[ 4], S32, 0x4bdecfa9); /* 38 */
  235.   HH ( c, d, a, b, in[ 7], S33, 0xf6bb4b60); /* 39 */
  236.   HH ( b, c, d, a, in[10], S34, 0xbebfbc70); /* 40 */
  237.   HH ( a, b, c, d, in[13], S31, 0x289b7ec6); /* 41 */
  238.   HH ( d, a, b, c, in[ 0], S32, 0xeaa127fa); /* 42 */
  239.   HH ( c, d, a, b, in[ 3], S33, 0xd4ef3085); /* 43 */
  240.   HH ( b, c, d, a, in[ 6], S34,  0x4881d05); /* 44 */
  241.   HH ( a, b, c, d, in[ 9], S31, 0xd9d4d039); /* 45 */
  242.   HH ( d, a, b, c, in[12], S32, 0xe6db99e5); /* 46 */
  243.   HH ( c, d, a, b, in[15], S33, 0x1fa27cf8); /* 47 */
  244.   HH ( b, c, d, a, in[ 2], S34, 0xc4ac5665); /* 48 */
  245.  
  246.   /* Round 4 */
  247. #define S41 6
  248. #define S42 10
  249. #define S43 15
  250. #define S44 21
  251.   II ( a, b, c, d, in[ 0], S41, 0xf4292244); /* 49 */
  252.   II ( d, a, b, c, in[ 7], S42, 0x432aff97); /* 50 */
  253.   II ( c, d, a, b, in[14], S43, 0xab9423a7); /* 51 */
  254.   II ( b, c, d, a, in[ 5], S44, 0xfc93a039); /* 52 */
  255.   II ( a, b, c, d, in[12], S41, 0x655b59c3); /* 53 */
  256.   II ( d, a, b, c, in[ 3], S42, 0x8f0ccc92); /* 54 */
  257.   II ( c, d, a, b, in[10], S43, 0xffeff47d); /* 55 */
  258.   II ( b, c, d, a, in[ 1], S44, 0x85845dd1); /* 56 */
  259.   II ( a, b, c, d, in[ 8], S41, 0x6fa87e4f); /* 57 */
  260.   II ( d, a, b, c, in[15], S42, 0xfe2ce6e0); /* 58 */
  261.   II ( c, d, a, b, in[ 6], S43, 0xa3014314); /* 59 */
  262.   II ( b, c, d, a, in[13], S44, 0x4e0811a1); /* 60 */
  263.   II ( a, b, c, d, in[ 4], S41, 0xf7537e82); /* 61 */
  264.   II ( d, a, b, c, in[11], S42, 0xbd3af235); /* 62 */
  265.   II ( c, d, a, b, in[ 2], S43, 0x2ad7d2bb); /* 63 */
  266.   II ( b, c, d, a, in[ 9], S44, 0xeb86d391); /* 64 */
  267.  
  268.   buf[0] += a;
  269.   buf[1] += b;
  270.   buf[2] += c;
  271.   buf[3] += d;
  272. }
  273.  
  274. /***********************************************************************
  275.  ** End of md5.c                                                      **
  276.  **********************************************************************/
  277.  
  278.  
  279.  
  280. [LISTIN╟ TWO]
  281.  
  282.  
  283. /***********************************************************************
  284.  ** md5.h -- header file for implementation of MD5                    **
  285.  ** RSA Data Security, Inc. MD5 Message-Digest Algorithm              **
  286.  ** Created: 2/17/90 RLR                                              **
  287.  ** Revised: 12/27/90 SRD,AJ,BSK,JT Reference C version               **
  288.  ** Revised (for MD5): RLR 4/27/91                                    **
  289.  **********************************************************************/
  290. /* typedef a 32-bit type */
  291. typedef unsigned long int UINT4;
  292.  
  293. /* Data structure for MD5 (Message-Digest) computation */
  294. typedef struct {
  295.   UINT4 i[2];                   /* number of _bits_ handled mod 2^64 */
  296.   UINT4 buf[4];                                    /* scratch buffer */
  297.   unsigned char in[64];                              /* input buffer */
  298.   unsigned char digest[16];     /* actual digest after MD5Final call */
  299. } MD5_CTX;
  300.  
  301. void MD5Init ();
  302. void MD5Update ();
  303. void MD5Final ();
  304.  
  305. /***********************************************************************
  306.  ** End of md5.h                                                      **
  307.  **********************************************************************/
  308.